/**
 * 
 */
package cn.micai.core.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.micai.common.web.session.SessionProvider;
import cn.micai.core.bean.user.Buyer;

/**
 * 处理上下文
 * 处理Session
 * 全局
 * 
 * @author 赵新国
 *
 */
public class SpringmvcInterceptor implements HandlerInterceptor{
	
	@Autowired
	private SessionProvider sessionProvider;
	
	private Integer adminId;
	
	//常量
	private static final String INTERCEPTOR_URL = "/buyer/";

	//方法前 /buyer/
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//管理员 初始化一个session 开发阶段
		/*if(adminId != null){
			Buyer buyer = new Buyer();
			buyer.setUsername("fbb2014");
			sessionProvider.setAttribute(request, Constants.BUYER_SESSION, buyer);
		}else{
			Buyer buyer = (Buyer) sessionProvider.getAttribute(request, Constants.BUYER_SESSION);
			boolean flag = false;
			if(null != buyer){
				flag = true;
			}
			request.setAttribute("isLogin", flag);
			
			//是否拦截 http://localhost:8080/buyer/index.shtml
			//	/buyer/index.shtml
			String requestURI = request.getRequestURI();
			if(requestURI.startsWith(INTERCEPTOR_URL)){
				//必须登录
				if(null == buyer){
					response.sendRedirect("/shopping/login.shtml?returnUrl=" + request.getParameter("returnUrl"));
					return false;
				}
			}
		}*/
		Buyer buyer = (Buyer) sessionProvider.getAttribute(request, Constants.BUYER_SESSION);
		boolean flag = false;
		if(null != buyer){
			flag = true;
			request.setAttribute("buyer", buyer);
		}
		request.setAttribute("isLogin", flag);

		//是否拦截 http://localhost:8080/buyer/index.shtml
		//	/buyer/index.shtml
		String requestURI = request.getRequestURI();
		if(requestURI.startsWith(INTERCEPTOR_URL)){
			//必须登录
			if(null == buyer){
				response.sendRedirect("/shopping/login.shtml?returnUrl=" + request.getParameter("returnUrl"));
				return false;
			}
		}
		return true;
	}

	//方法后
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	//页面渲染后
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	public void setAdminId(Integer adminId) {
		this.adminId = adminId;
	}

	
	
}
